package dbreflection.dbanalisys;

/*
import ConfigManagement.ConfigManager;
import DBRoutines.DBConnect.kDatabaseUser;
*/

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

import dbreflection.ConnectionManager;

/**
 * User: J0k3r
 * Date: 14.12.2006
 * Time: 9:10:20
 */
public class DBAnaliser
{
    // ������ ��������� ��..
    public static DBScheme Analise(String alias, String dbName)
    {
        // ������� ������ �����
        DBScheme scheme = new DBScheme(alias, null);

        Connection cons = ConnectionManager.getConnection(alias);
        try
        {
            ResultSet tables = cons.getMetaData().getTables(dbName, "", "", null);
            if (tables.first())
            {
                do
                {
                    // ������� ������ �������..
                    String tableCaption = null;
                    /*try{
                        tableCaption = ConfigManager.GetInstance().getSingleParam("Config.DBConfig." + kDatabaseUser.getActiveAlias() + "." + tables.getObject(3).toString() + ".nameTableAs");
                    }catch(Exception ex)
                    {*/
                        tableCaption = tables.getObject(3).toString();
//                    }
                    DBTable tab = new DBTable(tables.getObject(3).toString(), tableCaption, null);

                    System.out.println("Found table " + tables.getObject(3));

                    ResultSet columns = cons.getMetaData().getColumns(dbName, "", tab.getTableName(), null);
                    if (columns.first())
                    {
                        do
                        {
                            System.out.println("  Found Column " + columns.getObject(4));
                            tab.addColumn(new DBTableColumn(tab.getTableName(),
                                    columns.getObject(4).toString(),
                                    getColumnTypeByID(Integer.valueOf(columns.getObject(5).toString())),
                                    null,
                                    null));
                        }while (columns.next());
                    }

                    ResultSet eKeys = cons.getMetaData().getExportedKeys(dbName, null, tables.getObject(3).toString());
                    if (eKeys.first())
                    {
                        do
                        {
                            System.out.println("  Found exKey " + eKeys.getObject(7) + " -> " + eKeys.getObject(8));
                            ExportedKey exportedKey = new ExportedKey(eKeys.getObject(7).toString(),
                                    eKeys.getObject(3).toString(),
                                    eKeys.getObject(8).toString(),
                                    eKeys.getObject(4).toString(),
                                    eKeys.getObject(5).toString(),
                                    eKeys.getObject(1).toString());
                            tab.getColumns().get(eKeys.getObject(4).toString()).addExportedKey(exportedKey);
                        } while (eKeys.next());
                    }

                    ResultSet iKeys = cons.getMetaData().getImportedKeys(dbName, null, tables.getObject(3).toString());
                    if (iKeys.first())
                    {
                        do
                        {
                            System.out.println("  Found inKey " + iKeys.getObject(3) + " -> " + iKeys.getObject(4));
                            ImportedKey importedKey = new ImportedKey(iKeys.getObject(7).toString(),
                                    iKeys.getObject(3).toString(),
                                    iKeys.getObject(8).toString(),
                                    iKeys.getObject(4).toString(),
                                    iKeys.getObject(5).toString(),
                                    iKeys.getObject(1).toString());
                            tab.getColumns().get(iKeys.getObject(8).toString()).setImportedKey(importedKey);
                        } while (iKeys.next());
                    }
                    scheme.addTable(tab.getTableName(), tab);
                } while (tables.next());

            }
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return scheme;
    }

    public static Class getColumnTypeByID(Integer typeID)
    {
        switch(typeID)
        {
            case (4):
                return Integer.class;
            case (12):
                return String.class;
            case (7):
                return Float.class;
            case (91):
                return Date.class;
        }
        return Object.class;
    }
}
